package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.BidRequestAuditHistory;
import cn.wolfcode.p2p.business.mapper.BidRequestAuditHistoryMapper;
import cn.wolfcode.p2p.business.query.ExpAccountQueryObject;
import cn.wolfcode.p2p.business.service.IBidRequestAuditHistoryService;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IExpAccountService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 后台体验金发布
 */
@Controller
public class ExpAccountController {


    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private BidRequestAuditHistoryMapper auditHistoryMapper;

    //显示体验金发布页面
    @RequestMapping("expBidRequestPublish_list")
    public String expBidRequestList(Model model) {

        //最小借款金额
        model.addAttribute("minBidRequestAmount", Constants.BORROW_MIN_AMOUNT);

        //最小投标金额
        model.addAttribute("minBidAmount", Constants.BID_MIN_AMOUNT);

        //最小借款利率
        model.addAttribute("minCurrentRate", Constants.MIN_CURRENTRATE);

        //最大借款利率
        model.addAttribute("maxCurrentRate", Constants.MAX_CURRENTRATE);

        return "expbidrequest/expbidrequestpublish";
    }


    /**
     * 后台发布体验标
     *
     * @return
     */
    @RequestMapping("expBidRequestPublish")
    @ResponseBody
    public JsonResult expBidRequestPublish(BidRequest bidRequest) {

        bidRequestService.publishExpBidRequest(bidRequest);
        return JsonResult.instance();

    }

    @RequestMapping("expBidRequest_list")
    public String expBidRequestList(Model model, @ModelAttribute("qo") ExpAccountQueryObject qo) {

        qo.setBidRequestType(Constants.BIDREQUEST_TYPE_EXP);
        model.addAttribute("pageInfo", bidRequestService.listForExpPage(qo));
        return "expbidrequest/expbidrequestlist";
    }


    //后台体验标页面
    @RequestMapping("borrow_info")
    public String borrowInfo(Model model, Long id) {

        //新手标
        BidRequest bidRequest = bidRequestService.getById(id);
        model.addAttribute("bidRequest", bidRequest);
        List<BidRequestAuditHistory> br = auditHistoryMapper.selectBybidRequestId(bidRequest.getId());
        model.addAttribute("audits", br);

        return "expbidrequest/borrow_info";
    }


}
